//
//  EmergencyDetailTable.m
//  OperationPro
//
//  Created by yaxue wang on 2017/5/3.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "EmergencyDetailTable.h"
#import "EmergencyDetailTableCell.h"
#import "EmergencyDetailModel.h"
@interface EmergencyDetailTable()
@property(nonatomic,strong)NSString *uuid;
@property(nonatomic,strong)NSMutableDictionary *dataDic;
@property(nonatomic,strong)NSMutableArray *dataAr;

@end
@implementation EmergencyDetailTable

-(instancetype)initWithFrame:(CGRect)frame withUuid:(NSString*)Uuid
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setShowNoDataNotice:@"暂无内容"];
        self.uuid=Uuid;
        self.backgroundColor =BackColor;
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.dataDic=[[NSMutableDictionary alloc]init];
        self.dataAr =[[NSMutableArray alloc]init];
        self.showsVerticalScrollIndicator =NO;
        [self registerClass:[EmergencyDetailTableCell class] forCellReuseIdentifier:@"cell"];
        [self reloadDataTable];
        [self setTableActionType:CTableActionType_Push];

    }
    return self;
}
- (void)reloadDataTable
{
    [CClass HttpRequestTask:YES withUrl:@"emergency_getEmergencySignInDetail" withData:@{@"uuid":self.uuid} notifyError:YES onSuccess:^(BaseModel *model) {
        self.dataDic =(NSMutableDictionary*)model.B_data;
        self.dataAr = (NSMutableArray*)[self.dataDic objectForKey:@"user"];
        [self reloadData];
        self.tableHeaderView=[self headView];
        
    } onError:^(BaseModel *model) {
        
    }];
}

- (UIView*)headView{
    UIView *headView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 100)];
    headView.backgroundColor=BackColor;
    UIView *topView =[[UIView alloc]initWithFrame:CGRectMake(0, 6, WindowWidth, 56)];
    topView.backgroundColor=[UIColor whiteColor];
    [headView addSubview:topView];

    UIImageView *headImg=[[UIImageView alloc]initWithFrame:CGRectMake(30,7, 40, 40)];
    headImg.layer.masksToBounds =YES;
    headImg.layer.cornerRadius =20;
    //从本地找到
    headImg.image= [CClass GetImageFromLocal:NSUD_HeadImg];

    [topView addSubview:headImg];
   
    UILabel *nameLab =[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(headImg.frame)+10,0, 100, topView.bounds.size.height)];
    nameLab.font =F(14);
    nameLab.textColor=FontColor;
    nameLab.text=[CClass getCacheData:NSUD_Name];
    [topView addSubview:nameLab];
    UILabel *timeLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(nameLab.frame)+10, 0, WindowWidth-CGRectGetMaxX(nameLab.frame)-10,topView.bounds.size.height)];
    timeLab.textAlignment=NSTextAlignmentCenter;
    

    timeLab.textColor=MainColor;
    timeLab.font= F(12);
    [topView addSubview:timeLab];
    UILabel *lineLab=[[UILabel alloc]initWithFrame:CGRectMake(0, topView.bounds.size.height-0.5, WindowWidth, 0.5)];
    lineLab.backgroundColor=LineColor;
    [topView addSubview:lineLab];
    UIView *signView =[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(topView.frame), WindowWidth, 173)];
    signView.backgroundColor=BackColor;
    [headView addSubview:signView];
    UILabel *signBigLab = [[UILabel alloc]initWithFrame:CGRectMake((WindowWidth-112)/2, 30, 112,112)];
    signBigLab.layer.masksToBounds=YES;
    signBigLab.layer.cornerRadius=56;
    [signBigLab bk_whenTapped:^{
        //签到
        
        [CClass HttpRequestTask:YES withUrl:@"emergency_signIn" withData:@{@"userid":[CClass getCacheData:NSUD_Uuid],@"eyinform_uuid":self.uuid} notifyError:YES onSuccess:^(BaseModel *model) {
            //0.未签到，1.已经签到  STATUS
            if([[model.B_data objectForKey:@"STATUS"] isEqualToString:@"0"])
            {
                
            }else if ([[model.B_data objectForKey:@"STATUS"]isEqualToString:@"1"])
            {
                [CClass showMessage:@"签到成功"];
                [self reloadDataTable];

                
            }
        } onError:^(BaseModel *model) {
            
        }];
    }];
    [signView addSubview:signBigLab];
    
    UILabel *signSmallLab = [[UILabel alloc]initWithFrame:CGRectMake(6, 6, 100,100)];
    signSmallLab.layer.masksToBounds=YES;
    signSmallLab.layer.cornerRadius=50;
    signSmallLab.userInteractionEnabled=YES;
    signSmallLab.textColor=[UIColor whiteColor];
    signSmallLab.textAlignment=NSTextAlignmentCenter;
    signSmallLab.font=F(18);
    [signBigLab addSubview:signSmallLab];
    
    NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
    [dic setObject:[CClass getCacheData:NSUD_UUID] forKey:@"userid"];
    [dic setObject:self.uuid forKey:@"eyinform_uuid"];

    [CClass HttpRequestTask:YES withUrl:@"emergency_getUserSignInDetail" withData:dic notifyError:YES onSuccess:^(BaseModel *model) {
//        timeLab.text=[model.B_data objectForKey:@"created"];
        NSString *timeString = [[model.B_data objectForKey:@"created"]  substringWithRange:NSMakeRange(0, 16)];
        timeLab.text=timeString;
        //0.未签到，1.已经签到  STATUS
        if([[model.B_data objectForKey:@"STATUS"] isEqualToString:@"0"])
        {
            
            signBigLab.userInteractionEnabled=YES;
            signSmallLab.text=@"签到";
            signBigLab.backgroundColor =C(@"f6dbd3");
            signSmallLab.backgroundColor =MainColor;

            

        }else if ([[model.B_data objectForKey:@"STATUS"]isEqualToString:@"1"])
        {
            signBigLab.userInteractionEnabled=NO;
            signSmallLab.text=@"已签到";
            signSmallLab.backgroundColor =C(@"c6c6c6");
            signBigLab.backgroundColor =C(@"e5e9e9");


        }
        
    } onError:^(BaseModel *model) {
        
    }];


    UIView *contextView=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(signView.frame), WindowWidth, 100)];
    contextView.backgroundColor=[UIColor whiteColor];
    [headView addSubview:contextView];
   CGSize namesize=[[self.dataDic objectForKey:@"title"] sizeWithFont:F(15) constrainedToSize:CGSizeMake(WindowWidth-60, 900)];
    CGFloat height = MAX(namesize.height, 40);
    UILabel *contextLab=[[UILabel alloc]initWithFrame:CGRectMake(30, 0, WindowWidth-60, height)];
    contextLab.text =[self.dataDic objectForKey:@"title"];
    contextLab.numberOfLines=0;
    contextLab.textColor=FontColor;
    contextLab.font=F(15);
    [contextView addSubview:contextLab];
    UILabel *line1=[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(contextLab.frame), WindowWidth-30, 0.5)];
    line1.backgroundColor=LineColor;
    [contextView addSubview:line1];
    UILabel *startPlaceLab=[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(line1.frame), 70, 40)];
    startPlaceLab.text =@"发起时间";
    startPlaceLab.textColor=FontColor;
    startPlaceLab.font=F(15);
    [contextView addSubview:startPlaceLab];
    UILabel *startLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(startPlaceLab.frame), CGRectGetMaxY(line1.frame), WindowWidth-CGRectGetMaxX(startPlaceLab.frame)-30, 40)];
    startLab.text =[self.dataDic objectForKey:@"created"];
    startLab.textColor=FontColor;
    startLab.textAlignment=NSTextAlignmentRight;
    startLab.font=F(15);
    [contextView addSubview:startLab];
    UILabel *line2=[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(startLab.frame), WindowWidth-30, 0.5)];
    line2.backgroundColor=LineColor;
    [contextView addSubview:line2];

    UILabel *endPlaceLab=[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(line2.frame), 70, 40)];
    endPlaceLab.text =@"关闭时间";
    endPlaceLab.textColor=FontColor;
    endPlaceLab.font=F(15);
    [contextView addSubview:endPlaceLab];
    UILabel *endLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(endPlaceLab.frame), CGRectGetMaxY(line2.frame), WindowWidth-CGRectGetMaxX(endPlaceLab.frame)-30, 40)];
    if([F_NSpace([self.dataDic objectForKey:@"endtime"]) isEqualToString:@""])
    {
        endLab.text =@"";
 
    }else
    {
        endLab.text =[self.dataDic objectForKey:@"endtime"];

    }
    endLab.textColor=FontColor;
    endLab.textAlignment=NSTextAlignmentRight;
    endLab.font=F(15);
    [contextView addSubview:endLab];
    UILabel *line3=[[UILabel alloc]initWithFrame:CGRectMake(30, CGRectGetMaxY(endLab.frame), WindowWidth-30, 0.5)];
    line3.backgroundColor=LineColor;
    [contextView addSubview:line3];
    contextView.frame=CGRectMake(0, CGRectGetMaxY(signView.frame), WindowWidth, CGRectGetMaxY(line3.frame));
    
    UIView *numView=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(contextView.frame)+6, WindowWidth, 56)];
    numView.backgroundColor=[UIColor whiteColor];
    [headView addSubview:numView];
    
    UIImage*image1=[UIImage imageNamed:@"Ico_sige_already"];
    UIImageView *alreadyImg=[[UIImageView alloc]initWithFrame:CGRectMake(30,10, image1.size.width, image1.size.height)];
    alreadyImg.image= image1;
    [numView addSubview:alreadyImg];
    
    UILabel *alreadyLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(alreadyImg.frame)+8, 12, 45, 15)];
    alreadyLab.text =@"已签到";
    alreadyLab.textColor=FontColor;
    alreadyLab.font=F(13);
    [numView addSubview:alreadyLab];
    UILabel *alreadynumLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(alreadyImg.frame)+8, CGRectGetMaxY(alreadyLab.frame)+6, 80, 14)];
    alreadynumLab.text =[NSString stringWithFormat:@"%@人",[self.dataDic objectForKey:@"alReadySign"]];
    alreadynumLab.textColor=C(@"39a6f3");
    alreadynumLab.font=F(12);
    [numView addSubview:alreadynumLab];
    UILabel *centerLineLab=[[UILabel alloc]initWithFrame:CGRectMake(WindowWidth/2-0.5, 7, 0.5, numView.bounds.size.height-14)];
    centerLineLab.backgroundColor=LineColor;
    [numView addSubview:centerLineLab];
    UIImage*image2=[UIImage imageNamed:@"Ico_sige_no"];
    UIImageView *noImg=[[UIImageView alloc]initWithFrame:CGRectMake(30+CGRectGetMaxX(centerLineLab.frame),10, image2.size.width, image2.size.height)];
    noImg.image= image2;
    [numView addSubview:noImg];
    
    UILabel *noLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(noImg.frame)+8, 12, 45, 15)];
    noLab.text =@"未签到";
    noLab.textColor=FontColor;
    noLab.font=F(13);
    [numView addSubview:noLab];
    UILabel *nonumLab=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(noImg.frame)+8, CGRectGetMaxY(noLab.frame)+6, 80, 14)];
    nonumLab.text =[NSString stringWithFormat:@"%@人",[self.dataDic objectForKey:@"unSign"]];
    nonumLab.textColor=C(@"eb8c27");
    nonumLab.font=F(12);
    [numView addSubview:nonumLab];
    UIView *belowView=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(numView.frame)+6, WindowWidth, 40)];
    belowView.backgroundColor=[UIColor whiteColor];
    [headView addSubview:belowView];
    
    CGFloat width = WindowWidth/3;

    UILabel *lab1=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, width, 30)];
    lab1.text =@"地区";
    lab1.textColor=FontColor;
    lab1.textAlignment=NSTextAlignmentCenter;
    lab1.font=F(13);
    [belowView addSubview:lab1];
    UILabel *lab2=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab1.frame), 10, width, 30)];
    lab2.text =@"姓名";
    lab2.textColor=FontColor;
    lab2.font=F(13);
    lab2.textAlignment=NSTextAlignmentCenter;
    [belowView addSubview:lab2];
    UILabel *lab3=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(lab2.frame), 10, width, 30)];
    lab3.text =@"签到状态";
    lab3.textAlignment=NSTextAlignmentCenter;
    lab3.textColor=FontColor;
    lab3.font=F(13);
    [belowView addSubview:lab3];
    UILabel *line4=[[UILabel alloc]initWithFrame:CGRectMake(0, belowView.bounds.size.height-0.5, WindowWidth, 0.5)];
    line4.backgroundColor=LineColor;
    [belowView addSubview:line4];

    headView.frame=CGRectMake(0, 0, WindowWidth, CGRectGetMaxY(belowView.frame));
    return headView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSString *cellID = [NSString stringWithFormat:@"Cell_%@", NSStringFromClass([EmergencyDetailModel class])];
    EmergencyDetailTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[EmergencyDetailTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.contentView.backgroundColor=[UIColor whiteColor];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSMutableDictionary *dic=[self.dataAr objectAtIndex:indexPath.row];
    cell.locationLab.text=[dic objectForKey:@"user_city"];
    cell.nameLab.text=[dic objectForKey:@"name"];
    //0.未签到，1.已经签到
    if ([[dic objectForKey:@"es_status"] isEqualToString:@"0"]) {
        cell.stateLab.text=@"未签到";
        cell.stateLab.textColor =C(@"eb8c27");


    }else if ([[dic objectForKey:@"es_status"] isEqualToString:@"1"])
    {
        cell.stateLab.text=@"已签到";
        cell.stateLab.textColor =C(@"39a6f3");


    }

//    EmergencyDetailModel *model = [self.mainData objectAtIndex:indexPath.row];
//    [cell setModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
}


@end
